package com.链表;



/**
 * 自定义链表
 */

public class CustomLink {
    //节点类
   static class Node{
        //数据
        private String data;
        //指向下一个节点
        private Node next;
        //构造器
        public Node(String data){
            this.data = data;
        }

        /**
         * 递归添加节点
         * @param node
         */
        public void addNode(Node node){

            if(this.next == null){
                this.next = node;
            }else{
                this.next.addNode(node);
            }
        }

        /**
         * 递归查询
         * @param data
         * @return
         */
        public boolean containNode(String data){
            if(data.equals(this.data)){
                return true;
            }else{
                //判断下一个连表是否为空
                if(null != this.next){
                    return this.next.containNode(data);
                }else{
                    return false;
                }
            }
        }

        /**
         * 递归打印节点
         */
        public void pringNode(){
            System.out.println(data);
            if(this.next != null){
                this.next.pringNode();
            }
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }
    //link类
    static class Link{
       //头
       private  Node head;
       //数量
       private int count = 0 ;
       //索引
       private int index = 0;

        /**
         * 递归添加
         * @param data
         */
       public void add(String data){
           //不能添加空元素
           if(null == data){
               return;
           }
           Node newNode = new Node(data);
           if(this.head == null){
               this.head = newNode;
           }else{
                this.head.addNode(newNode);
           }
           //自增
           this.count ++;
       }

        /**
         * 获取数量
         * @return
         */
       public int getSize(){
           return this.count;
       }

        /**
         * 判断链表是否为空
         * @return
         */
       public boolean isEmpty(){
           return 0 == this.count;
       }

        /**
         * 递归查询
         * @param data
         * @return
         */
       public boolean contains(String data){
           if(null == data || null == this.head){
               return false;
           } else{

            return this.head.containNode(data);}
       }

        /**
         * 打印链表
         */
       public void print(){
           if(this.head != null){
               this.head.pringNode();
           }
       }
    }
    /**
     * 递归方法
     * @param node
     */
    public static void getNode(Node node){
       if(node == null){
           return;
       }

        System.out.println(node.getData());
       getNode(node.getNext());
    }
    public static void main(String[] args) {
        Node head = new Node("头头");
        Node hand = new Node("手");
        Node footer = new Node("脚");
        head.setNext(hand);
        hand.setNext(footer);

        //方法一:循环取出数据
        Node node = head;
        while (node != null){
            System.out.println(node.getData());
            node = node.getNext();
        }
        System.out.println("==========递归============");
        //方法二 递归
        Node digui = head;
        getNode(digui);
        System.out.println("============添加============");
        Link link = new Link();
        link.add("one");
        link.add("two");
        link.add("three");
        link.print();
        System.out.println(link.getSize());
        System.out.println(link.isEmpty());
        System.out.println(link.contains("three"));

    }
}
